#!/bin/bash

# 首先确保curl静态库已经构建完成
if [ ! -f "./build/curl/universal/lib/libcurl.a" ]; then
    echo "请先运行 build_curl_static.sh 构建curl静态库"
    exit 1
fi

# 加载环境变量
source ./build_env.sh

# Framework名称和版本
FRAMEWORK_NAME="curl"
FRAMEWORK_VERSION="A"
FRAMEWORK_DIR="$BUILD_DIR/${FRAMEWORK_NAME}.framework"

# 清理旧的Framework
rm -rf "$FRAMEWORK_DIR"

# 创建Framework目录结构
mkdir -p "$FRAMEWORK_DIR/Versions/$FRAMEWORK_VERSION/Headers"
mkdir -p "$FRAMEWORK_DIR/Versions/$FRAMEWORK_VERSION/Resources"

# 创建符号链接
ln -s "$FRAMEWORK_VERSION" "$FRAMEWORK_DIR/Versions/Current"
ln -s "Versions/Current/Headers" "$FRAMEWORK_DIR/Headers"
ln -s "Versions/Current/Resources" "$FRAMEWORK_DIR/Resources"
ln -s "Versions/Current/${FRAMEWORK_NAME}" "$FRAMEWORK_DIR/${FRAMEWORK_NAME}"

# 复制头文件
cp -R "$BUILD_DIR/curl/universal/include/curl" "$FRAMEWORK_DIR/Headers/"

# 复制通用库到Framework
cp "$BUILD_DIR/curl/universal/lib/libcurl.a" "$FRAMEWORK_DIR/Versions/$FRAMEWORK_VERSION/${FRAMEWORK_NAME}"

# 创建Info.plist文件
cat > "$FRAMEWORK_DIR/Resources/Info.plist" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleExecutable</key>
    <string>${FRAMEWORK_NAME}</string>
    <key>CFBundleIdentifier</key>
    <string>se.haxx.curl</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>${FRAMEWORK_NAME}</string>
    <key>CFBundlePackageType</key>
    <string>FMWK</string>
    <key>CFBundleShortVersionString</key>
    <string>7.88.0</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>7.88.0</string>
    <key>MinimumOSVersion</key>
    <string>${IPHONEOS_DEPLOYMENT_TARGET}</string>
    <key>CFBundleSupportedPlatforms</key>
    <array>
        <string>iPhoneOS</string>
        <string>iPhoneSimulator</string>
    </array>
</dict>
</plist>
EOF

# 设置权限
chmod -R 755 "$FRAMEWORK_DIR"

echo "Framework创建完成: $FRAMEWORK_DIR"